package com.orange.base.component.biztool.simulateddata.service.impl;

import com.orange.base.component.biztool.simulateddata.service.SimulatedDataParam;
import com.orange.base.component.biztool.simulateddata.util.Pinyin;
import com.orange.base.component.biztool.simulateddata.util.PinyinException;

/**
 * 拼音
 *
 * @author biankudingcha
 * @date: 2022-08-24
 * @since 0.5.0
 */
public class PinyinStrategy extends AbstractSimulatedData {

    private Pinyin pinyin;

    public String getType() {
        return "pinyin";
    }

    public String getCaption() {
        return "拼音";
    }

    @Override
    public Object getValue(SimulatedDataParam param) {
        String field = (String) param.getValue();
        String value = (String) param.getRow().get(field);
        if (!isEmpty(value)) {
            Pinyin pinyin_ = getPinyin();
            return pinyin_.translateFirstChar(value).toUpperCase();
        }
        return null;
    }

    private Pinyin getPinyin() {
        if (this.pinyin != null) {
            return this.pinyin;
        }
        try {
            this.pinyin = new Pinyin();
            return this.pinyin;
        } catch (PinyinException e) {

        }
        return null;
    }

}
